<?php

	// TITLE: Account AUTO MODEL
	// FILE: scaffolding/model/account.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createAccount = function($con, $account) {
		$sql = 'INSERT INTO account (id, username, password, date, status, is_admin, is_office, person_id) VALUES (';
		$sql .= $account->getId() . ', ';
		$sql .= $account->getUsername() . ', ';
		$sql .= $account->getPassword() . ', ';
		$sql .= $account->getDate() . ', ';
		$sql .= $account->getStatus() . ', ';
		$sql .= $account->getIsAdmin() . ', ';
		$sql .= $account->getIsOffice() . ', ';
		$sql .= $account->getPersonId() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readAccount = function($con, $id) {
		$sql = 'SELECT * FROM account WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$account = new Account();
			$account->setId(result[0]);
			$account->setUsername(result[1]);
			$account->setPassword(result[2]);
			$account->setDate(result[3]);
			$account->setStatus(result[4]);
			$account->setIsAdmin(result[5]);
			$account->setIsOffice(result[6]);
			$account->setPersonId($dbio->select($readPerson(result[7])));
		} else {
			$account = false;
		}
		return $account;
	};

	$updateAccount = function($con, $account) {
		$sql = 'UPDATE account SET ';
		$sql .= 'id = ' . $account->getId() . ', ';
		$sql .= 'username = ' . $account->getUsername() . ', ';
		$sql .= 'password = ' . $account->getPassword() . ', ';
		$sql .= 'date = ' . $account->getDate() . ', ';
		$sql .= 'status = ' . $account->getStatus() . ', ';
		$sql .= 'is_admin = ' . $account->getIsAdmin() . ', ';
		$sql .= 'is_office = ' . $account->getIsOffice() . ', ';
		$sql .= 'person_id = ' . $account->getPersonId() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteAccount = function($con, $id) {
		$sql = 'DELETE FROM account WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listAccount = function($con) {
		$sql = 'SELECT * FROM account';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$accounts = array();
			while ($result = mysqli_fetch_array($results)) {
				$account = new Account();
				$account->setId(result[0]);
				$account->setUsername(result[1]);
				$account->setPassword(result[2]);
				$account->setDate(result[3]);
				$account->setStatus(result[4]);
				$account->setIsAdmin(result[5]);
				$account->setIsOffice(result[6]);
				$account->setPersonId($dbio->select($readPerson(result[7])));
				$accounts[] = account;
			}// end while
		} else {
			$accounts = false;
		}
		return $accounts;
	};

